<?php
const appKey = [
    "mxnzp"=>[
        "appId"=>"rouki8jpzp1uankj",
        "appSecret"=>"dzErSVcyamVQV2hpeTlYMHlNWGxWUT09"
    ],
    "haah"=>[
        "key"=>"0KWQRhVBnTRE2ZBMwfV04MpcEr"
    ]
];
class post
{

    public static function bilibili_video():bool
    {
        @mkdir("cache");
        $link = args_post("link");
        if (!strpos("bilibili.com/",$link)){
            $link = "https://www.bilibili.com/video/".$link;
        }
        $link = base64_encode($link);
        $url = "https://www.mxnzp.com/api/bilibili/video?url=$link&app_id=".appKey["mxnzp"]["appId"]."&app_secret=".appKey["mxnzp"]["appSecret"];
        $result = file_get_contents($url);
        $array = json_decode($result,true);
        $cache_name = "cache/".time();
        file_put_contents($cache_name,file_get_contents($array["data"]["cover"]));
        $hash_name = "cache/".hash_file("sha256",$cache_name);
        rename($cache_name,$hash_name);
        $array["data"]["cover"]=$hash_name;
        $link = $array["data"]["list"][0]["url"];
        $index = strpos($link,"bilivideo.com");
        $array["data"]["list"][0]["url"]="https://proxy.tools.xcgzs.cn/".substr($link,$index+strlen("bilivideo.com")+1);
        $result=json_encode($array);
        echo $result;
        return true;
    }

    public static function douyin_video():bool
    {
        @mkdir("cache");
        $link = base64_encode(args_post("link"));
        $url = "https://www.mxnzp.com/api/douyin/video?url=$link&app_id=".appKey["mxnzp"]["appId"]."&app_secret=".appKey["mxnzp"]["appSecret"];
        $result = file_get_contents($url);
        $array = json_decode($result,true);
        $link = $array["data"]["url"];
        $index = strpos($link,"douyinvod.com");
//        $array["data"]["url"]="https://douyin.proxy.tools.xcgzs.cn/".substr($link,$index+strlen("douyinvod.com")+1);
        $result = json_encode($array);
        echo $result;
        return true;
    }

    public static function qq_info():bool
    {
        $qq = args_post("qq");
        $url = "https://v.api.aa1.cn/api/qqjson/index.php?qq=$qq";
        $result = file_get_contents($url);
        echo $result;
        return true;
    }

    public static function qq_chat():bool
    {
        return true;
    }

    public static function qq_group():bool
    {
        return true;
    }

    public static function qq_card():bool
    {
        return true;
    }

    public static function qq_pc_online():bool
    {
        $qq = args_post("qq");
        $url = "https://api.vvhan.com/api/qqcode?qq=$qq";
        $result = file_get_contents($url);
        echo $result;
        return true;
    }

    public static function site_title():bool
    {
        $link = args_post("link");
        $url = "https://api.vvhan.com/api/title?url=$link";
        $result = file_get_contents($url);
        echo $result;
        return true;
    }

    public static function site_logo():bool
    {
        $link = args_post("link");
        $cache_name = "cache/".time();
        $url = "https://api.haah.net/api/ico/?key=".appKey["haah"]["key"]."&url=$link";
        file_put_contents($cache_name,file_get_contents($url));
        $hash_name = "cache/".hash_file("sha256",$cache_name);
        rename($cache_name,$hash_name);
        echo "http://".$_SERVER["HTTP_HOST"]."/".$hash_name;
        return true;
    }

    public static function netease_music():bool
    {
        $id = args_post("id");
        $url = "https://api.vvhan.com/api/music?id=$id&type=song&media=netease";
        $result = file_get_contents($url);
        echo $result;
        return true;
    }

    public static function netease_music_hot():bool
    {
        $url = "https://api.vvhan.com/api/reping";
        $result = file_get_contents($url);
        $result = json_decode($result,true);
        $id = $result["data"]["id"];
        $result["data"]["content"]=str_replace("\n","<br>",$result["data"]["content"]);
        $result_info = json_decode(file_get_contents("https://api.vvhan.com/api/music?id=$id&type=song&media=netease"),true);
        if ($result_info["success"]){
            $result["data"]["songName"]=$result_info["name"];
            $result["data"]["mp3url"]=$result_info["mp3url"];
        }
        $result = json_encode($result);
        echo $result;
        return true;
    }



    
    /*API处理部分*/
    public static function apiQQ_card():bool
    {
        echo json_encode(["status"=>false,"msg"=>apiMsg[1001]]);
        return true;
    }

    public static function apiQQ_info():bool
    {
        $qq = args_request("qq");
        $url = "https://v.api.aa1.cn/api/qqjson/index.php?qq=$qq";
        $result = file_get_contents($url);
        if (strpos($result,"JSON数据：")>-1){
            $result = str_replace("JSON数据：","",$result);
            $result = json_decode($result,true);
            echo json_encode(["status"=>true,"data"=>["name"=>$result[0]["nickname"],"avatar"=>$result[0]["touxiang"],"mail"=>$result[0]["email"]]]);
        }else{
            echo json_encode(["status"=>false,"msg"=>apiMsg[1002]]);
        }
        return true;
    }

    public static function apiQQ_chat():bool
    {
        echo json_encode(["status"=>false,"msg"=>apiMsg[1001]]);
        return true;
    }

    public static function apiQQ_group():bool
    {
        echo json_encode(["status"=>false,"msg"=>apiMsg[1001]]);
        return true;
    }

    public static function apiQQ_pc_online():bool
    {
        $qq = args_request("qq");
        $url = "https://api.vvhan.com/api/qqcode?qq=$qq";
        $result = file_get_contents($url);
        $result = json_decode($result,true);
        if ($result["success"]){
            echo json_encode(["status"=>true,"data"=>["state"=>$result["state"]]]);
        }else{
            echo json_encode(["status"=>true,"msg"=>$result["message"]]);
        }
        return true;
    }
}